﻿using BLL;
using MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loginter
{
    public partial class TruckInfoList : System.Web.UI.Page
    {
        //调用BLL层车辆信息
        SelectTruckBLL truck = new SelectTruckBLL();
        //调用BLL层的车队有关内容
        TruckTeamBLL truckteam = new TruckTeamBLL();
        //调用BLL层的车辆信息
        TruckBLL truckb = new TruckBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindtruckList();
                BindcheliangList();
                BindTruckInfo();
            }
        }

        /// <summary>
        /// 绑定所属车队列表
        /// </summary>
        private void BindtruckList()
        {
            DataTable table = truckteam.GetTruckList();
            drop_Truck.DataSource = table;
            drop_Truck.DataTextField = "TeamName";
            drop_Truck.DataValueField = "TeamID";
            drop_Truck.DataBind();
            drop_Truck.Items.Insert(0, new ListItem("不限", "0"));
        }
        /// <summary>
        /// 绑定车辆类型
        /// </summary>
        private void BindcheliangList()
        {
            DataTable table = truck.GetTruckList();
            drop_Type.DataSource = table;
            drop_Type.DataTextField = "Type";
            drop_Type.DataValueField = "Type";
            drop_Type.DataBind();
            drop_Type.Items.Insert(0, new ListItem("--全部--", "0"));
        }

        private void BindTruckInfo()
        {
            //所属车队
            int teamid = Convert.ToInt32(drop_Truck.SelectedValue);
            //车牌号码
            string number = txt_Number.Text.Trim();
            //车辆类型
            string type = drop_Type.SelectedItem.ToString();
            //车辆状态
            int state = Convert.ToInt32(drop_State.SelectedValue);
            //是否删除
            int isdelte = Convert.ToInt32(rblDelete.SelectedValue);

            TruckTeamQueryModel querymodel = new TruckTeamQueryModel()
            {
                FK_TeamID = teamid,
                Number = number,
                Type = type,
                State = state
            };
            List<TruckMODEL> list = truckb.GetTruckList(querymodel);
            if (list == null)
            {
                Console.Write("数据源为空！");
            }
            gr_TruckTime.DataSource = list;
            gr_TruckTime.DataBind();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Select_Click(object sender, EventArgs e)
        {
            BindTruckInfo();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddTruckList.aspx");
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gr_TruckTime_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                int userid = Convert.ToInt32(e.CommandArgument);
                int result = truckb.DeleteTruckList(userid);
                if (result == 1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除成功！');</script>");
                    BindTruckInfo();
                }
                else if (result == -1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('该信息已删除！');</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除失败！');</script>");
                }
            }
        }

        protected void gr_TruckTime_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }
    }
}